package net.mikoo.seals.agent.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.agent.biz.services.CompanyServices;
import net.mikoo.seals.agent.biz.services.DevicesServices;
import net.mikoo.seals.agent.biz.services.MemberServices;
import net.mikoo.seals.agent.common.BaseController;
import net.mikoo.seals.agent.common.PageModel;
import net.mikoo.seals.agent.common.enums.CompanyStatusEnum;
import net.mikoo.seals.agent.common.exception.ParametersException;
import net.mikoo.seals.agent.common.helper.FormHelper;
import net.mikoo.seals.agent.model.Company;
import net.mikoo.seals.agent.model.CompanyQuery;
import net.mikoo.seals.agent.model.Devices;
import net.mikoo.seals.agent.model.DevicesQuery;
import net.mikoo.seals.agent.model.Party;

@Controller
@RequestMapping("/admin/company")
public class CompanyController extends BaseController {
	
	Logger logger = LoggerFactory.getLogger(CompanyController.class);
	
	@Autowired
	private CompanyServices companyServices;
	
	@Autowired
	private MemberServices memberServices;
	
	@Autowired
	private DevicesServices devicesServices;
	
	

	
	
	@RequestMapping("/list")
	public String list(CompanyQuery query, HttpServletRequest request, Model model) {
		
		PageModel<Company> pm  = companyServices.loadByQueryToPage(query);
		model.addAttribute("pm", pm);
		
		return resultView(ADMIN_VIEW, "company/list");
	}
	

	@RequestMapping("/addInfo")
	public String addInfo() {
		
		
		
		return resultView(ADMIN_VIEW, "company/addInfo");
	}
	
	
	@RequestMapping("/addAccount")
	public String addAccount(Long companyId,Model model) {
		
		Company company = companyServices.loadByPrimaryKey(companyId);
		if(company == null) {
			return redirect("/admin/conpany/addInfo.htm");
		}
		
		String companyStatus = company.getCompanyStatus();
		if(!companyStatus.equals(CompanyStatusEnum.CREATED.code())) {
			return redirect("/admin/conpany/info.htm?companyId="+companyId);
		}
		model.addAttribute("company", company);
		
		return resultView(ADMIN_VIEW, "company/addAccount");
	}
	
	
	@RequestMapping("/addSuccess")
	public String addSuccess(Long companyId,Model model) {
		
		return resultView(ADMIN_VIEW, "company/addSuccess");
	}
	
	
	@RequestMapping("/createAccount")
	@ResponseBody
	public Map<String,Object> createAccount(HttpServletRequest request) {
		
		try {
			
			FormHelper help = new FormHelper();
			Party party = help.getCreatOperator(request);
			
			party = memberServices.create(party);
			
			Map<String,Object> data = new HashMap<>();
			data.put("companyId", party.getCompanyId());
			
			return result(SUCCESS, "添加成功");
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
	}
	
	
	@RequestMapping("/create")
	@ResponseBody
	public Map<String,Object> create(Company company,HttpServletRequest request, Model model) {
		try {
			companyServices.save(company);
			
			Map<String,Object> data = new HashMap<>();
			data.put("companyId", company.getCompanyId());
			return result(SUCCESS, "添加成功",data);
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
	}
	
	/**
	 * 公司详情
	 * @param companyId
	 * @param model
	 * @return
	 */
	@RequestMapping("/info")
	public String info(Long companyId, Model model) {
		Company company = companyServices.loadByPrimaryKey(companyId);
		model.addAttribute("company", company);
		return resultView(ADMIN_VIEW, "company/info");
	}
	
	/**
	 * 公司管理者
	 * @param companyId
	 * @param model
	 * @return
	 */
	@RequestMapping("/manager")
	public String manager(Long companyId, Model model) {
		
		Company company = companyServices.loadByPrimaryKey(companyId);
		Party party = memberServices.getByUserLoginId(company.getUserLoginId());
		model.addAttribute("company", company);
		model.addAttribute("party", party);
		return resultView(ADMIN_VIEW, "company/manager");
	}
	
	/**
	 * 公司设备列表
	 * @param query
	 * @param model
	 * @return
	 */
	@RequestMapping("/devices")
	public String devices(DevicesQuery query, Model model) {
		
		PageModel<Devices> pm = devicesServices.getPageByQuery(query);
		model.addAttribute("pm", pm);
		return resultView(ADMIN_VIEW, "company/devices");
	}
	

	@RequestMapping("/edit")
	public String edit(Long companyId, Model model) {
		Company company = companyServices.loadByPrimaryKey(companyId);
		model.addAttribute("company", company);
		return resultView(ADMIN_VIEW, "company/edit");
	}
	
	
	@RequestMapping("/modify")
	public Map<String,Object> modify(Company company, Model model) {
		
		try {
			companyServices.modifyByPrimaryKey(company);
			return result(SUCCESS, "添加成功");
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
		
	}

}
